﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    class Player : BaseObject
    {
        public bool m_bJumping; 
        public bool m_bDucking;
        public bool m_bDashing;
        public bool m_bIsOnLand;
        public bool m_bAttacking;

        public List<Equip> m_lEquips = new List<Equip>();

        public Player()
        {
            // This is a comment
            // The below statement is true
            // The above statement is false
            // You just blew my mind
            m_ObjectType = OBJ_TYPE.OBJ_PLAYER;

            
        }

        override public void Update(float deltaT , KeyboardState LastKeyState)
        {
            m_Position.Y += m_Velocity.Y * deltaT;
            m_Position.X += m_Velocity.X * deltaT * ((m_bJumping) ? 2.5f : 1) * ((m_bDashing && !m_bJumping) ? 2.0f : 1);

            if (m_Position.Y > 600-1)
            {
                m_Position.Y = 600;
                m_Velocity.Y = 0.0f;
                if (m_bJumping == true)
                {
                    m_bJumping = false;
                    m_bIsOnLand = true;
                }
            }
            else if (!m_bIsOnLand)
            {
                m_Velocity.Y += 6.81f;
                m_bJumping = true;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space) && LastKeyState.IsKeyUp(Keys.Space) && m_bJumping == false)
            {
                m_Position.Y -= 25;
                m_Velocity.Y = -130.0f;
                m_bJumping = true;
                m_bIsOnLand = false;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.LeftShift))
            {
                m_bDashing = true;
            }
            else
            {
                m_bDashing = false;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Left) && LastKeyState.IsKeyUp(Keys.Left))
            {
                m_bDucking = false;
                m_Velocity.X = -25;
                m_Direction = SpriteEffects.FlipHorizontally;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right) && LastKeyState.IsKeyUp(Keys.Right))
            {
                m_bDucking = false;
                m_Velocity.X = 25;
                m_Direction = SpriteEffects.None;
            }
            else if (!Keyboard.GetState().IsKeyDown(Keys.Right) && !Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                {
                    m_bDucking = true;
                }
                else
                {
                    m_bDucking = false;
                }
                m_Velocity.X *= 0.8f;
                if ((m_Velocity.X <= 0.99f && m_Velocity.X > 0.0) || (m_Velocity.X >= -0.99f && m_Velocity.X < 0.0))
                {
                    m_Velocity.X = 0.0f;
                }
            }

            if (Keyboard.GetState().IsKeyDown(Keys.A) && LastKeyState.IsKeyUp(Keys.A))
            {
                m_bAttacking = true;
                m_fFrame = 0.0f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S) && LastKeyState.IsKeyUp(Keys.S))
            {
                Random rand = new Random();
                foreach (Equip p in m_lEquips)
                {
                    p.m_cColor.B = (byte)(rand.Next(255));
                    p.m_cColor.G = (byte)(rand.Next(255));
                    p.m_cColor.R = (byte)(rand.Next(255));
                }
            }

            if (m_bAttacking == true)
            {
                m_AnimOrigin.X = 0;
                m_nMaxFrame = 3;
                m_AnimOrigin.Y = 256;
            }
            else if (m_bJumping == true)
            {
                m_AnimOrigin.X = 128;
                m_nMaxFrame = 1;
                m_AnimOrigin.Y = 0;
            }
            else if (m_bDucking == true)
            {
                m_nMaxFrame = 1;
                m_AnimOrigin.X = 256;
                m_AnimOrigin.Y = 0;
            }
            else if (m_Velocity.X != 0.0f)
            {
                m_nMaxFrame = 4;
                m_AnimOrigin.X = 0;
                m_AnimOrigin.Y = 128;
            }
            else
            {
                m_nMaxFrame = 1;
                m_AnimOrigin.X = 0;
                m_AnimOrigin.Y = 0;
            }

            if (m_bAttacking == true)
            {
                m_fFrame += deltaT;
            }
            else
            {
                m_fFrame += deltaT * ((m_Velocity.X > 0.0f) ? m_Velocity.X : -m_Velocity.X) / 25.0f * ((m_bDashing && !m_bJumping) ? 1.5f : 1);
            }
            if (m_fFrame >= m_nMaxFrame)
            {
                m_fFrame = 0;
                if (m_bAttacking == true)
                {
                    m_bAttacking = false;
                }
            }
            m_bIsOnLand = false;
        }

        override public void Render(SpriteBatch spriteBatch, Camera cam)
        {
            spriteBatch.Draw(m_Texture, new Rectangle((int)((m_Position.X - cam.m_Position.X) * cam.m_fZoom), (int)((m_Position.Y - cam.m_Position.Y) * cam.m_fZoom), (int)(128 * cam.m_fZoom), (int)(128 * cam.m_fZoom)), new Rectangle(128 * (int)m_fFrame + (int)m_AnimOrigin.X, (int)m_AnimOrigin.Y, 128, 128), Color.White, 0.0f, new Vector2(64, 128), m_Direction, 0.0f);
            foreach (Equip p in m_lEquips)
            {
                spriteBatch.Draw(p.m_Texture, new Rectangle((int)((m_Position.X - cam.m_Position.X) * cam.m_fZoom), (int)((m_Position.Y - cam.m_Position.Y) * cam.m_fZoom), (int)(128 * cam.m_fZoom), (int)(128 * cam.m_fZoom)), new Rectangle(128 * (int)m_fFrame + (int)m_AnimOrigin.X, (int)m_AnimOrigin.Y, 128, 128), p.m_cColor, 0.0f, new Vector2(64, 128), m_Direction, 0.0f);
            }
        }

        public override void CollisionResponse(BaseObject other, Rectangle collRect)
        {
            if (other.m_ObjectType == OBJ_TYPE.OBJ_ENEMY)
            {
                if (collRect.Width > collRect.Height)
                {
                    if (m_Velocity.Y >= 0.0f)
                    {
                        m_bIsOnLand = true;
                        m_bJumping = false;
                    }
                    if (collRect.Y < m_Position.Y)
                        m_Position -= new Vector2(0.0f, collRect.Height);
                    else
                        m_Position += new Vector2(0.0f, collRect.Height);
                }
                else
                {
                    if (collRect.X > m_Position.X)
                        m_Position -= new Vector2(collRect.Width, 0.0f);
                    else
                        m_Position += new Vector2(collRect.Width, 0.0f);
                }
            }
        }

    }
}
